//
//  YZPigGroupDocDeathVC.m
//  VET
//
//  Created by YZ on 2018/7/21.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigGroupDocDeathVC.h"
#import "YZPigGroupDocDeathViewModel.h"
#import "YZPigGroupDocDeathCell.h"
#import "YZBaseRefreshTableView.h"

#import "YZRouter+AutoVC.h"
#import "NSDate+YZDatePickView.h"

@interface YZPigGroupDocDeathVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) YZPigGroupDocDeathViewModel *viewModel;

@property (nonatomic, strong) YZBaseRefreshTableView *contentTb;

@end

@implementation YZPigGroupDocDeathVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self addObserver];
    [self.contentTb headerBeginRefresh];
}

- (void)configUI
{
    [self.view addSubview:self.contentTb];
    
    YZWeakSelf(self);
    self.contentTb.headerRefreshBlock = ^{
        [weakself.contentTb resetState];
        [weakself.viewModel headerRefreshSuccess:^(id obj) {
            NSLog(@"obj:%ld",((NSArray *)obj).count);
            [weakself.contentTb reloadData];
            [weakself.contentTb headerEndRefresh];
        } fail:^(NSError *error) {
            [weakself.contentTb headerEndRefresh];
        }];
    };
    
    self.contentTb.footerRefreshBlock = ^{
        [weakself.viewModel footerRefreshSuccess:^(NSArray *obj) {
            if (obj.count == 0 && [weakself.viewModel isNoMoreData]) {
                [weakself.contentTb showNoMoreData];
                return;
            }
            [weakself.contentTb reloadData];
            [weakself.contentTb footerEndRefresh];
        } fail:^(NSError *error) {
            [weakself.contentTb footerEndRefresh];
        }];
    };
}

- (void)addObserver
{
    [RACObserve(self.viewModel, loading) subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [SVProgressHUD show];
        }else
        {
            [SVProgressHUD dismiss];
        }
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    static NSString *cellId = @"YZPigGroupDocDeathCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[YZPigGroupDocDeathCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    ((YZPigGroupDocDeathCell *)cell).model = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZPigGroupDocDeathModel *model = (YZPigGroupDocDeathModel*)[self.viewModel modelInSection:indexPath.section row:indexPath.row];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:kText(self.batchNo) forKey:@"batchNo"];
    [params setObject:model.vetDieoutVo.dieoutTime forKey:@"dieoutTime"];
    [params setObject:model.vetDieoutVo.dieoutNum forKey:@"dieoutNum"];
    [params setObject:model.vetDieoutVo.dieoutCause forKey:@"dieoutCause"];
    [params setObject:model.vetDieoutVo.remark forKey:@"remark"];
    [[YZRouter sharedInstance] yz_atuoPush:@"infoDeadSwine" params:@{@"params" : params, @"isUnEdit" : @"1"}];
}


#pragma mark -- setters && getters

- (YZBaseRefreshTableView *)contentTb
{
    if(!_contentTb){
        _contentTb = [[YZBaseRefreshTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTopViewHeight - YZ_STATUSBAR_HEIGHT - kCurrentDeviceNum(50) - kCurrentDeviceNum(20) - YZ_BOTTOM_MARGIN) style:UITableViewStylePlain];
        _contentTb.tableFooterView = [UIView new];
        _contentTb.delegate = self;
        _contentTb.dataSource = self;
        _contentTb.estimatedRowHeight = 30;
        _contentTb.estimatedSectionHeaderHeight = 0;
        _contentTb.estimatedSectionFooterHeight = 0;
    }
    return _contentTb;
}

- (YZPigGroupDocDeathViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [YZPigGroupDocDeathViewModel new];
        _viewModel.swineryId = self.swineryId;
    }
    return _viewModel;
}


@end
